package org.dromara.business.service.impl;

import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.BusOutsourceBo;
import org.dromara.business.domain.vo.BusOutsourceVo;
import org.dromara.business.domain.BusOutsource;
import org.dromara.business.mapper.BusOutsourceMapper;
import org.dromara.business.service.IBusOutsourceService;

import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 资源Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-24
 */
@RequiredArgsConstructor
@Service
public class BusOutsourceServiceImpl implements IBusOutsourceService {

    private final BusOutsourceMapper baseMapper;

    /**
     * 查询资源
     *
     * @param id 主键
     * @return 资源
     */
    @Override
    public BusOutsourceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询资源列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 资源分页列表
     */
    @Override
    public TableDataInfo<BusOutsourceVo> queryPageList(BusOutsourceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BusOutsource> lqw = buildQueryWrapper(bo);
        Page<BusOutsourceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的资源列表
     *
     * @param bo 查询条件
     * @return 资源列表
     */
    @Override
    public List<BusOutsourceVo> queryList(BusOutsourceBo bo) {
        LambdaQueryWrapper<BusOutsource> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BusOutsource> buildQueryWrapper(BusOutsourceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BusOutsource> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSubscribeId()), BusOutsource::getSubscribeId, bo.getSubscribeId());
        lqw.eq(StringUtils.isNotBlank(bo.getSelfUrl()), BusOutsource::getSelfUrl, bo.getSelfUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), BusOutsource::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getPostUrl()), BusOutsource::getPostUrl, bo.getPostUrl());
        lqw.eq(bo.getPublishTime() != null, BusOutsource::getPublishTime, bo.getPublishTime());
        lqw.like(StringUtils.isNotBlank(bo.getAuthorName()), BusOutsource::getAuthorName, bo.getAuthorName());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthorUrl()), BusOutsource::getAuthorUrl, bo.getAuthorUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), BusOutsource::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getContactEncryption()), BusOutsource::getContactEncryption, bo.getContactEncryption());
        lqw.eq(StringUtils.isNotBlank(bo.getContact()), BusOutsource::getContact, bo.getContact());
        return lqw;
    }

    /**
     * 新增资源
     *
     * @param bo 资源
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BusOutsourceBo bo) {
        BusOutsource add = MapstructUtils.convert(bo, BusOutsource.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 同步资源
     *
     * @return 是否同步成功
     */
    @Override
    public Boolean sync() {
        try {
            URL feedUrl = new URL("https://www.v2ex.com/feed/outsourcing.xml");
            SyndFeedInput input = new SyndFeedInput();
            SyndFeed feed = input.build(new XmlReader(feedUrl));

            System.out.println("Feed Title: " + feed.getTitle());
            System.out.println("Feed Description: " + feed.getDescription());

            feed.getEntries().forEach(entry -> {
                System.out.println("Entry Title: " + entry.getTitle());
                System.out.println("Entry Link: " + entry.getLink());
                System.out.println("Entry Description: " + entry.getDescription());
                System.out.println("-------------------");
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 修改资源
     *
     * @param bo 资源
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BusOutsourceBo bo) {
        BusOutsource update = MapstructUtils.convert(bo, BusOutsource.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BusOutsource entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除资源信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
